import MSData
import WorkerMSAPI
import WorkerMSDB
import MSLogger
import MSDBTemplates
#import WorkerPCNFirebird

import time
import os
import sys
import heartbeating

heartbeat = heartbeating.HeartbeatListener()
heartbeat.start()

MSLogger.logInit('C:\\safe\\ms.new.log', 5, True)
MSLogger.logInfo('ms driver started')
reader = WorkerMSDB.Worker('SRV1\PGSMDATA', '49177', 'PGSMData', 'sa', 'q123456*')
reader.connect()

#writer = WorkerPCNFirebird.Worker('192.168.0.120:C:\\pcn6\\data\\DATA.GDB', 'SYSDBA', 'masterkey')
#writer2 = WorkerPCNFirebird.Worker('192.168.0.121:C:\\pcn6\\data\\DATA.GDB', 'SYSDBA', 'masterkey')
#writer.connect()
#writer2.connect()

objects = []
MSDBTemplates.GET_MSObjects_FROM_CarOnline(reader.connection.cursor(), objects)
ctr = 0

try:
    while True:
        if ctr == 6:
            MSDBTemplates.GET_MSObjects_FROM_CarOnline(reader.connection.cursor(), objects)
            ctr = 0
        reader.readEventsPGSM(objects)
        reader.readEventsContactId(objects)
        reader.readEventsBolid(objects)
        #writer.writeEventsStatic(objects)
        #writer2.writeEventsStatic(objects)
        #reader.finalyze(objects)
        ctr += 1
        time.sleep(10)
except KeyboardInterrupt:
    MSLogger.logError('ms daemon stopped')
    heartbeat.stop()
    #os.unlink('C:\\safe\\ms.pid')
except Exception, err:
    shr = str(err)[0:100]
    MSLogger.logError('ms driver fatal error: %s' % (str(err)))
    heartbeat.stop()
    #os.unlink('C:\\safe\\ms.pid')

